package net.bracode.view;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import net.bracode.dao.Database;

public class MenuPrincipal extends Application{

	private AnchorPane pane;
	private Scene scene;
	private Button inserir;
	private Button mostrar;
	private Button atualizar;
	private Button remover;
	private Button exibir;
	private Button obter;
	
	private Stage stage;
	
	public MenuPrincipal() {
		Database.createDatabase();
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		iniciarComponentes();
		this.stage = stage;
		
		pane.getChildren().addAll(inserir, mostrar, atualizar, remover, exibir, obter);
		
		inserir.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				try {
					new ParticipanteInsert(MenuPrincipal.this).start(getStage());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		mostrar.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				try {
					new ParticipanteShow(MenuPrincipal.this).start(getStage());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		
		scene.getStylesheets().add("login.css");
		stage.setScene(scene);
		stage.setTitle("Menu Principal");
		stage.show();
		iniciarLayouts();
	}
	
	public static void main(String[] args) {
		launch(args);
	}

	public Stage getStage() {
		return stage;
	}
	
	private void iniciarComponentes(){
		pane      = new AnchorPane();
		scene     = new Scene(pane, 600, 400);
		inserir   = new Button("Cadastrar Participante");
		mostrar   = new Button("Mostrar Participantes");
		atualizar = new Button("Atualizar Participante");
		remover   = new Button("Remover Participante");
		exibir    = new Button("Exibir Participante");
		obter     = new Button("Obter Participantes");
	}
	
	private void iniciarLayouts(){
		mostrar.setLayoutX((pane.getWidth() - mostrar.getWidth()) / 2.0f);
		mostrar.setLayoutY(100);
		
		inserir.setLayoutX((pane.getWidth() - inserir.getWidth()) / 2.0f);
		inserir.setLayoutY(50);
		
		atualizar.setLayoutX((pane.getWidth() - atualizar.getWidth()) / 2.0f);
		atualizar.setLayoutY(150);
		
		remover.setLayoutX((pane.getWidth() - remover.getWidth()) / 2.0f);
		remover.setLayoutY(200);
		
		exibir.setLayoutX((pane.getWidth() - exibir.getWidth()) / 2.0f);
		exibir.setLayoutY(250);
		
		obter.setLayoutX((pane.getWidth() - obter.getWidth()) / 2.0f);
		obter.setLayoutY(300);
	}
}
